package com.ncmmall.application.business.distribution;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.distribution.DistributionSetting;
import com.ncmmall.domain.business.distribution.DistributionSettingRepository;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;

@Service
@Transactional
public class DistributionSettingApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private DistributionSettingRepository settingRepository;

    /**
     * 修改合伙人申请
     *
     * @param applyNote 合伙人申请内容
     * @param user      修改人
     */
    public void modifyApplyNote(String applyNote, WebUser user) {
        DistributionSetting old = findLastSetting();
        DistributionSetting setting = new DistributionSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }
        setting.setApplyNote(applyNote);
        setting.setCreateTime(new Timestamp(System.currentTimeMillis()));
        setting.setUser(user);
        settingRepository.save(setting);
    }

    public DistributionSetting findLastSetting() {
        return queryChannelService.queryOneEntity("select * from distribution_setting order by createTime desc limit 1", new HashMap<>(), DistributionSetting.class);
    }

    /**
     * 分销设置
     *
     * @param params 设置
     */
    public void modifyDistribution(DistributionSetting params, WebUser user) {

        DistributionSetting old = findLastSetting();
        DistributionSetting setting = new DistributionSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }

        setting.setId(null);
        setting.setCreateTime(new Timestamp(System.currentTimeMillis()));
        setting.setUser(user);

        if (params.getEnable() != null) {
            setting.setEnable(params.getEnable());
        }

        if (params.getDistributorCheck() != null) {
            setting.setDistributorCheck(params.getDistributorCheck());
        }

        if (params.getDistributorAutoOpenShop() != null) {
            setting.setDistributorAutoOpenShop(params.getDistributorAutoOpenShop());
        }

        if (params.getType() != null) {
            setting.setType(params.getType());
        }

        if (params.getMemberBindValid() != null) {
            setting.setMemberBindValid(params.getMemberBindValid());
        }

        if (params.getPerformanceAttribution() != null) {
            setting.setPerformanceAttribution(params.getPerformanceAttribution());
        }

        settingRepository.save(setting);

    }


    /**
     * 结算设置
     *
     * @param params 设置
     */
    public void modifySettle(DistributionSetting params, WebUser user) {

        Requires.isTrue(params.getMinPrice() != null && params.getMinPrice() >= 0, "请填写正确的单笔提现最小金额");
        Requires.isTrue(params.getMaxPrice() != null && params.getMaxPrice() >= params.getMinPrice(), "请填写正确的单笔提现最大金额");
        Requires.isTrue(params.getWithdrawNumPerDay() != null && params.getWithdrawNumPerDay() >= 0, "请填写正确的每天可提现次数");
        Requires.isTrue(params.getWithdrawNumPerMonth() != null && params.getWithdrawNumPerMonth() >= params.getWithdrawNumPerDay(), "请填写正确的每月可提现次数");
        Requires.isTrue(params.getDistributionDuration() != null && params.getDistributionDuration() >= 0, "请填写正确的佣金分成时间");
        Requires.isTrue(params.getCommissionPayType() != null, "请选择佣金支付方式");

        DistributionSetting old = findLastSetting();
        DistributionSetting setting = new DistributionSetting();
        if (old != null) {
            BeanUtils.copyProperties(old, setting);
        }

        setting.setId(null);
        setting.setCreateTime(new Timestamp(System.currentTimeMillis()));
        setting.setUser(user);
        setting.setMinPrice(params.getMinPrice());
        setting.setMaxPrice(params.getMaxPrice());
        setting.setWithdrawNumPerDay(params.getWithdrawNumPerDay());
        setting.setWithdrawNumPerMonth(params.getWithdrawNumPerMonth());
        setting.setDistributionDuration(params.getDistributionDuration());
        setting.setCommissionPayType(params.getCommissionPayType());

        settingRepository.save(setting);

    }

    /**
     * 分销设置修改
     *
     * @param type   1、分销设置
     *               2、结算设置
     * @param params 修改的内容
     * @param user   修改人
     */
    public void modify(int type, DistributionSetting params, WebUser user) {
        if (type == 1) {
            modifyDistribution(params, user);
        } else if (type == 2) {
            modifySettle(params, user);
        } else {
            throw new IllegalArgumentException("参数异常");
        }
    }
}
